<?php

namespace app\common\model;

use think\Model;
use think\Db;

class Base extends Model {

    protected $createTime = 'create_time';

    /**
     * @param  array $map 查询过滤
     * @param  integer $page 分页值
     * @param  string $order 排序参数
     * @param  string $field 结果字段
     * @param  integer $page_number 每页数量
     * @return 结果集
     */
    public function getListByPage($map = [], $page_number = 10, $field = true, $order = 'id desc') {
        $list = $this->where($map)->order($order)->field($field, true)->paginate($page_number)->toArray();
        return $list;
    }

    public function getToListByPage($map = [], $page_number = 10, $field = false, $order = 'id desc') {
        $list = $this->where($map)->order($order)->field($field, false)->paginate($page_number)->toArray();
        return $list;
    }
    /**
     * 新增或编辑数据
     * @param  array/object  $data 来源数据
     * @param  boolean $kv   主键值
     * @param  boolean $uid  校验用户
     * @param  string  $key  主键名
     * @return [type]        执行结果
     */
    public function editData($data, $kv = false, $uid = false, $key = 'id', $confirm = false) {
        $this->allowField(true);
        if ($confirm) {
            //是否验证
            $this->validate($confirm);
        }
        if ($kv) {
            //编辑
            $arr = [$key => $kv];
            $uid ? $data['uid'] = $uid : [$key => $kv];
            $res = $this->save($data, $arr);
            return $res;
        } else {
            $res = $this->data($data)->save();
            return $this->id;
        }
    }
}
